
import javax.swing.*;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;
import java.awt.print.PrinterException;

/**
 * DrawGnomon.java This class will draw the gnomon of a sundial based the value
 * passed in
 *
 * @author Arun Lee, Michael Terada, Marek Lopez
 *
 */
public class DrawGnomon extends JFrame implements Printable {

    double gnomon;
    // Just in case the user is in the southern hemishpere
    // Gui might include instructions to flip gnomon, rendering this useless
    boolean flipGnomon;
    int x1 = 10;
    int y1 = 45;
    int x2 = x1 + 30;
    int y2 = y1 + 300;

    //default; should never be executed due to error handling in Ics314ui.java
    public DrawGnomon() {
        this.gnomon = -0.0;
        setTitle("Gnomon Error");
        setSize(600, 600);
        setVisible(true);
        setBackground(Color.white);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public DrawGnomon(double gnomonIn) {
        this.gnomon = Math.abs(gnomonIn); //Gnomon should always be positive

        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        setTitle("Gnomon");
        setSize(size);
        setVisible(true);
        setBackground(Color.white);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        if (gnomon < 0.0) {
            flipGnomon = true;
        } else {
            flipGnomon = false;
        }

    }

    public void paint(Graphics canvas) {
        //create rectangle to attach gnomon to base
        double gnomonRadians = gnomon * Math.PI / 180;

        double factor = (y2 - y1) / Math.cos(gnomonRadians);

        int endGnomonX = (int) (x2 + factor * Math.sin(gnomonRadians));
        int endGnomonY = (int) (y1 + factor * Math.cos(gnomonRadians));

        canvas.drawLine(x1, y1, x1, y2);
        canvas.drawLine(x1, y1, x2, y1);
        canvas.drawLine(x1, y2, x2, y2);
        canvas.drawLine(x2, y1, x2, y2); //also serves as the base of the triangle
        //label flap
        canvas.drawString("A", (int) x2 / 2, (int) y2 / 2);

        //draw the gnomon
        //Hypotenuse
        canvas.drawLine(x2, y1, endGnomonX, endGnomonY);
        //The height
        canvas.drawLine(x2, y2, endGnomonX, endGnomonY);

    }

    //Returns the length of gnomon in pixels
    //Hour lines in DrawFace.java should be as long as gnomon length
    public int getLength() {
        int length = this.y2 - this.y1;
        return length;
    }

    /**
     * Required by Printable interface.
     *
     * @param g
     * @param pf
     * @param page
     * @return
     * @throws PrinterException
     */
    public int print(Graphics g, PageFormat pf, int page)
            throws PrinterException {

        // We have only one page, and 'page'
        // is zero-based
        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        // User (0,0) is typically outside the
        // imageable area, so we must translate
        // by the X and Y values in the PageFormat
        // to avoid clipping.
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        // Now we perform our rendering
        //This prints a grey page with the applet name at the top, but no lines.
        //why no lines?
        this.paint(g2d);

        // tell the caller that this page is part
        // of the printed document
        return PAGE_EXISTS;
    }
}